package com.leyou.mq;

import com.leyou.service.PageService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.common.mq.MQConstants.Exchange.ITEM_EXCHANGE_NAME;
import static com.leyou.common.mq.MQConstants.Queue.PAGE_ITEM_DOWN;
import static com.leyou.common.mq.MQConstants.Queue.PAGE_ITEM_UP;
import static com.leyou.common.mq.MQConstants.RoutingKey.ITEM_DOWN_KEY;
import static com.leyou.common.mq.MQConstants.RoutingKey.ITEM_UP_KEY;

@Component
public class ItemListener {

    @Autowired
    private PageService pageService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = PAGE_ITEM_UP,durable = "true"),  //durable = "true"：用于将消息持久化
            exchange = @Exchange(name = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = ITEM_UP_KEY
    ))
    public void listenInsert(Long spuId) {
        if (spuId != null) {
            // 新增
            pageService.createItemHtml(spuId);
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = PAGE_ITEM_DOWN,durable = "true"),  //durable = "true"：用于将消息持久化
            exchange = @Exchange(name = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = ITEM_DOWN_KEY
    ))
    public void listenDelete(Long spuId) {
        if (spuId != null) {
            // 删除
            pageService.deleteItemHtml(spuId);
        }
    }
}
